define([], function () { 

 var handsontable = avalon.define({
          $id: "handsontable",
          txt: "kindeditor widget", 
          getData:function(e) {
                  console.log("test");
                  var $container = $("#handsontable"); 
                  console.log($container.data("handsontable").getData());
                  handsontable.txt = JSON.stringify({"data": $container.data("handsontable").getData()}); 
            },
          sliderightshow:function(e){
            handsontable.$fire("all!sliderightshow",'50%');
          },
          addfile:function(e){
            handsontable.$fire("all!msglist","上传文件"); 
          },
          copy:function(e){
            alert("请选中内容后按ctrl + c 键复制");
          },
          paste:function(e){
            alert("请选中内容后按ctrl + v 键粘贴");
          }
      });


    $("#handsontable").handsontable({
      rowHeaders: true,
      colHeaders: true,
      colWidths:150,
      rowHeights:40,
      manualColumnResize: true,
      manualRowResize: true,
      autoWrapCol:true,
      autoWrapRow:true, 
      minSpareRows:3,
      minSpareCols:1,
      minRows: 33,
      minCols: 10,
      //stretchH: 'all',
      //fixedRowsTop: 2,
      //fixedColumnsLeft: 2,
      //contextMenu: true,
      contextMenu: {
          callback: function (key, options) {
            if (key === 'upload') {
              setTimeout(function () {
                //timeout is used to make sure the menu collapsed before alert is shown
                handsontable.$fire("all!uploadfilelist","上传文件"); 
              }, 100);
            }
          },
          items: {
            // "row_above": {
            //   disabled: function () {
            //     //if first row, disable this option
            //     return ($("#handsontable").handsontable('getSelected')[0] === 0);
            //   }
            // },
            // "row_below": {},
            // "hsep1": "---------",
            // "remove_row": {
            //   name: 'Remove this row, ok?',
            //   disabled: function () {
            //     //if first row, disable this option
            //     return ($("#handsontable").handsontable('getSelected')[0] === 0);
            //   }
            // },
            // "hsep2": "---------",
            "upload": {name: '上传文件'}
          }
        }
      // contextMenuCopyPaste: {
      //   swfPath: "/swf/ZeroClipboard.swf"
      // }
    });

    //实例化大屏  

    $("#handsontablebig").handsontable({ 
          rowHeaders: true,
          colHeaders: true,
          minRows: 30,
          minCols: 30,
          manualColumnResize: true,
          manualRowResize: true,
          autoWrapCol:true,
          autoWrapRow:true, 
          minSpareRows:3,
          minSpareCols:1, 
          contextMenu: {
            callback: function (key, options) {
              if (key === 'upload') {
                setTimeout(function () {
                  //timeout is used to make sure the menu collapsed before alert is shown
                  handsontable.$fire("all!uploadfilelist","上传文件"); 
                }, 100);
              }
            },
            items: { 
              "upload": {name: '上传文件'}
            }
         }
    }); 

      var slideright = avalon.define({
         $id: "slideright",
         sliderightdisplay:false,
         sliderightlayoutWidth: 0,
         closeslideright:function(e){
             slideright.sliderightdisplay = false;
             slideright.sliderightlayoutWidth = 0;
         }
     });

    slideright.$watch('sliderightshow', function(v) {
         console.log(v);
          slideright.sliderightdisplay = true;
          slideright.sliderightlayoutWidth = '80%';
          //slideright.sliderightlayoutWidth = 500;  

          // var loadData =  function () { 
          //   return $("#handsontable").data("handsontable").getData(); 
          // }
          // $("#example1").handsontable('loadData', loadData);  
    });
 
     var uploaddialog = avalon.define("upload_dialog", function(vm) {
          //vm.message = "笑傻了吧！！"; 
          vm.$upOpts = {
              title: "上传文件",
              showClose: false,
              //container: "modalWrapper"
          }
          vm.show = function( id ){
              //console.log(id);
              avalon.vmodels[id].toggle = true;
 
              //progressbar----------------------------------------------
               var v = 0 , timer
                setTimeout(function() {
                    timer = setInterval(function() {
                        v++
                        if(v > 100) return clearInterval(timer)
                        avalon.vmodels.$uploaddialogprogressbar.value = v
                    }, 50)
                }, 1000)
              //progressbar----------------------------------------------end
          }
      
          //progressbar----------------------------------------------
          vm.progressbar = {
              onComplete: function() {
                  avalon.log(this)
                  avalon.log('this bar is complete')
              }
          }
          vm.$skipArray = ["progressbar"]
          //progressbar----------------------------------------------end
          
      });

     uploaddialog.$watch("uploadfilelist", function(v) {
          console.log("uploadfilelist");
          uploaddialog.uploadtitle = v;
          avalon.vmodels["upload"].zIndex = 300; 
          uploaddialog.show("upload"); 
          initupload(); 
     });

      

     function initupload(){//实例化上传组件
          var uploader = Qiniu.uploader({ 
              runtimes: 'html5,flash,html4',
              browse_button: 'pickfiles',
              container: 'filelist',
              drop_element: 'filelist',
              max_file_size: '100mb',
              flash_swf_url: 'js/plupload/Moxie.swf',
              dragdrop: true,
              // x_vars:{
              //     'domid' : fileareaid ,
              //     'pid':proid
              // },//自定义变量
              //unique_names:true,
              save_key: true,
              //chunk_size: '4mb',
              uptoken_url: "/makeupopen",
              domain: "/",
              auto_start: true,
              init: {
                  'FilesAdded': function(up, files) {
                         
                      var uploadhtml = '';
                      plupload.each(files, function(file) { 
                          uploadhtml+='<div class="progress" id="' + file.id + '"><div class="progress-bar" style="width: 10%;" id="st' + file.id +'">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div></div>';  
                          up.start();
                      });  
                      dialogInstance.setMessage(uploadhtml);
                      dialogInstance.open();
                  },
                  'BeforeUpload': function(up, file) {
                      // var progress = new FileProgress(file, 'fsUploadProgress');
                      // var chunk_size = plupload.parseSize(this.getOption('chunk_size'));
                      // if (up.runtime === 'html5' && chunk_size) {
                      //     progress.setChunkProgess(chunk_size);
                      // }
                  },
                  'UploadProgress': function(up, file) {
                      // var progress = new FileProgress(file, 'fsUploadProgress');
                      // var chunk_size = plupload.parseSize(this.getOption('chunk_size'));

                      // progress.setProgress(file.percent + "%", file.speed, chunk_size);
                      //console.log(file.id);
                      document.getElementById(file.id).getElementsByTagName('b')[0].innerHTML = '<span>' + file.percent + "%</span>";
                      $("#st"+file.id).css("width",file.percent+"%");
                  },
                  'UploadComplete': function() {
                      //$('#success').show();
                      dialogInstance.close();
                  },
                  'FileUploaded': function(up, file, info) {
                      // var progress = new FileProgress(file, 'fsUploadProgress');
                      // progress.setComplete(up, info);

                      //console.log(info);
                      var obj = jQuery.parseJSON(info);
                       if (obj.status =="ok"){
                            getResult(obj);//获得上传的文件路径
                          }else{
                            alert(info.message);
                          } 
                      $('#'+file.id).delay(6000).remove();
                     // getresult(fileareaid);
                  },
                  'Error': function(up, err, errTip) {
                      //alert(errTip);
                      $.scojs_message.options.delay = 3000;
                      $.scojs_message('请求失败 Error '+err, $.scojs_message.TYPE_ERROR);
                      //$('table').show();
                      // var progress = new FileProgress(err.file, 'fsUploadProgress');
                      // progress.setError();
                      // progress.setStatus(errTip);
                  }
              }
          });
     }
 
    
 

});


